<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="${column.comments}" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="name">
      <el-input v-model="dataForm.name" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="sm1">
      <el-input v-model="dataForm.sm1" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="sm2">
      <el-input v-model="dataForm.sm2" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="sm3">
      <el-input v-model="dataForm.sm3" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="sm4">
      <el-input v-model="dataForm.sm4" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="appUniqueId">
      <el-input v-model="dataForm.appUniqueId" placeholder="${column.comments}"></el-input>
    </el-form-item>
    <el-form-item label="${column.comments}" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="${column.comments}"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          appId: 0,
          deptId: '',
          name: '',
          sm1: '',
          sm2: '',
          sm3: '',
          sm4: '',
          createTime: '',
          delFlag: '',
          appUniqueId: '',
          remark: ''
        },
        dataRule: {
          deptId: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          sm1: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          sm2: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          sm3: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          sm4: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          appUniqueId: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '${column.comments}不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.appId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.appId) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysapplication/info/${this.dataForm.appId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptId = data.sysApplication.deptId
                this.dataForm.name = data.sysApplication.name
                this.dataForm.sm1 = data.sysApplication.sm1
                this.dataForm.sm2 = data.sysApplication.sm2
                this.dataForm.sm3 = data.sysApplication.sm3
                this.dataForm.sm4 = data.sysApplication.sm4
                this.dataForm.createTime = data.sysApplication.createTime
                this.dataForm.delFlag = data.sysApplication.delFlag
                this.dataForm.appUniqueId = data.sysApplication.appUniqueId
                this.dataForm.remark = data.sysApplication.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysapplication/${!this.dataForm.appId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'appId': this.dataForm.appId || undefined,
                'deptId': this.dataForm.deptId,
                'name': this.dataForm.name,
                'sm1': this.dataForm.sm1,
                'sm2': this.dataForm.sm2,
                'sm3': this.dataForm.sm3,
                'sm4': this.dataForm.sm4,
                'createTime': this.dataForm.createTime,
                'delFlag': this.dataForm.delFlag,
                'appUniqueId': this.dataForm.appUniqueId,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
